package falcon.server.processor;

import falcon.common.Bash;
import falcon.common.Errno;
import falcon.common.FalconHost;
import falcon.server.FalconByteBufHandler;
import falcon.server.FalconProManager;

public final class FalconBashProcessor {
	
	public static void processExec(FalconProManager manager, FalconProManager.Req req, String[] pa) {
		int errno;
		Bash bash;
		String cmd = pa[0];
		String host = pa[1];
		if ("localhost".equals(host)) {
			bash = new Bash(cmd);
		} else {
			int port = Integer.parseInt(pa[2]);
			String user = pa[3];
			String[] rs = new String[1];
			errno = FalconHost.get(manager.getConfig(), host, port, user, rs);
			if (errno != Errno.SUCCESS) {
				req.errno = errno;
				req.desc= "验证主机信息失败";
				return;
			}
			String password = rs[0];
			bash = new Bash(cmd, host, port, user, password);
		}
		StringBuilder result = new StringBuilder(512);
		bash.bindOutput(result);
		errno = bash.execute();
		req.desc = result.toString();
		req.errno = errno;
	}
}
